package cn.bulibuqi.myblog.dao;

import cn.bulibuqi.myblog.bean.Blog;
import cn.bulibuqi.myblog.bean.Tag;
import cn.bulibuqi.myblog.bean.Type;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.sf.jsqlparser.schema.Column;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author bulibuqi
 * @create 2021-12-22:56
 */
@Mapper
public interface MyBlogMapper extends BaseMapper<Blog> {
//    @Select("SELECT * FROM `type` where ${ew.sqlSegment}")
//    List<Type> getTypeList(@Param("ew") QueryWrapper wrapper);




    @Select("SELECT DISTINCT * FROM blog_tag " +
            "JOIN blog  ON blog_tag.blog_id=blog_tag.blog_id " +
            "WHERE blog.id=#{blogId}")
    List<Tag> getBlogOfTag(String blogId);


//    1(name = #{ew.paramNameValuePairs.MPGENVAL1} AND id > #{ew.paramNameValuePairs.MPGENVAL2})
//      必须是ew

    @Select("select DISTINCT * from blog left join blog_tag on blog.id=blog_tag.blog_id " +
            "join `type` on blog.type_id=`type`.id " +
            "where ${ew.sqlSegment}")

    List<Blog> getBlogInfo(@Param("ew")QueryWrapper queryWrapper);


    @Select("select * from blog")
    Page<Blog> getMyAllBlogs(Page<?> page);


//    @Result(column="hero_id",property="leagues",
//            many=@Many(
//                    select="cn.stylefeng.guns.modular.dotaunderlords.mapper.DotaLeagueMapper.getListByHeroId"
//            )




    @Select("select blog.*  from blog left join blog_tag on blog.id=blog_tag.blog_id " +
            "join `type` on blog.type_id=`type`.id " +
            "where ${ew.sqlSegment} "+
                "group by blog.id")
    @Result(column = "type_id",property = "type",
            many = @Many(
                    select = "cn.bulibuqi.myblog.dao.MyBlogMapper.getType"
            )
    )
    @Result(column = "id",property = "id")
    @Result(column = "id",property = "tags",
        many = @Many(
                select = "cn.bulibuqi.myblog.dao.MyBlogMapper.getBlogOfTagById"
        )
    )
    Page<Blog> getPageBlogInfo(Page<?> page, @Param("ew")QueryWrapper queryWrapper);




    @Select("  select tag.* " +
                    "from blog_tag join tag " +
                        "on blog_tag.`tag_id`=tag.`id` " +
                            "where blog_tag.`blog_id`=#{blog_id}")
    List<Tag> getBlogOfTagById(@Param("blog_id") Integer id);



    @Select("select blog.* from blog where blog.id=#{id}")
    @Result(column = "id",property = "id")
    @Result(column = "id",property = "tags",
            many = @Many(
                    select = "cn.bulibuqi.myblog.dao.MyBlogMapper.getBlogOfTagById"
            )
    )
    @Result(column = "type_id",property = "typeId")
    @Result(column = "type_id",property = "type",
            many = @Many(
                    select = "cn.bulibuqi.myblog.dao.MyBlogMapper.getType"
            )
    )
    Blog getAllBlogInfoById( @Param("id")Long id);






    @Select("select * from type where id=#{id}")
    Type getType(@Param("id")Long id);


    @Select("SELECT COUNT(tag_id) count,tag_id FROM blog_tag " +
            "WHERE tag_id=#{id} " +
            "GROUP BY tag_id")

    Integer getTagCount(@Param("id")Long id);














}
